package com.iot.core.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.iot.core.constant.ResponseEnum;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageDO<T> implements Serializable {

	private String msg = ResponseEnum.SUCCESS.getMsg();
	private Integer code = ResponseEnum.SUCCESS.getCode();
	private PageSO pageSO;
	private Map<String,Object> result = new HashMap<>();

	public static PageDO error(String msg) {
		PageDO res = new PageDO();
		res.setCode(ResponseEnum.ERROR.getCode());
		res.setMsg(msg);
		return res;
	}

	public void setRows(List<T> rows) {
		result.put("rows", rows);
	}

	@JsonIgnore
	public List<T> getRows() {
		Object rows = result.get("rows");
		if (rows == null) {
			return new ArrayList<T>();
		}
		return (List<T>) rows;
	}

	public void setTotal(Integer total) {
		result.put("total", total);
	}

	@JsonIgnore
	public Integer getTotal() {
		Object total = result.get("total");
		if (total == null) {
			return 0;
		}
		return Integer.parseInt(total.toString());
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public void setOffset(Integer offset) {
		result.put("offset", offset);
	}

	@JsonIgnore
	public Integer getOffset() {
		Object offset = result.get("offset");
		if (offset == null) {
			return 0;
		}
		return Integer.parseInt(offset.toString());
	}

	public void setLimit(Integer limit) {
		result.put("limit", limit);
	}

	@JsonIgnore
	public Integer getLimit() {
		Object limit = result.get("limit");
		if (limit == null) {
			return 0;
		}
		return Integer.parseInt(limit.toString());
	}

	@JsonIgnore
	public PageSO getPageSO() {
		return pageSO;
	}

	public void setPageSO(PageSO pageSO) {
		this.pageSO = pageSO;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public PageDO() {
	}

	public PageDO(String msg, Integer code, List<T> rows, Integer total) {
		this.msg = msg;
		this.code = code;
		this.setRows(rows);
		this.setTotal(total);
	}

	@Override
	public String toString() {
		return "PageDO{" +
				"msg='" + msg + '\'' +
				", code=" + code +
				", pageSO=" + pageSO +
				", result=" + result +
				'}';
	}
}
